package com.example.prison.controller;

import com.example.prison.config.Restful;
import com.example.prison.entity.VisCardMessage;
import com.example.prison.entity.VisReservation;
import com.example.prison.entity.VisitorAppointment;
import com.example.prison.entity.VisitorList;
import com.example.prison.service.VisitorAppointmentService;
import com.example.prison.service.VisitorListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Api(tags = "访客设备对接")
@RestController
@RequestMapping("/api")
public class VisitorProcessor {

    @Autowired
    private VisitorAppointmentService visitorAppointmentService;

    @Autowired
    private VisitorListService visitorRecordListService;

    @PostMapping("/visReservation/add")
    public Restful<VisReservation> add(@RequestBody VisReservation vis,
                                       @RequestParam String apitoken) {
        System.out.println(vis);
        System.out.println(apitoken);
        System.out.println("新增访客信息");
        return Restful.success("新增访客信息");
    }

    @ApiOperation("设备端上传访客人证记录")
    @PostMapping("/v1/recordvisit")
    public Restful<VisReservation> recordVisit(@RequestBody VisCardMessage vis) {
        log.info("设备端上传访客人证记录:"+vis);
        try {
            //更新访客状态
            VisitorAppointment visitor = visitorAppointmentService.getByIDCardNumber(vis.getUser_number());
            visitor.setVisitorStatus("已入场");
            //新增访客记录
            VisitorList record = new VisitorList();
            record.setVisitorName(vis.getUser_name());
            record.setVisitorCard(vis.getUser_number());
            record.setVisitorTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            visitorRecordListService.add(record);
        } catch (Exception e) {
            log.info("设备端上传访客人证记录失败！"+vis);
        }
        return Restful.success("新增访客预约");
    }

//    @ApiOperation("设备端上传访客人证记录")
//    @PostMapping("/v1/recordvisit")
//    public Restful<VisReservation> recordVisit(@RequestParam("object") String object) {
//        log.info("设备端上传访客人证记录:"+object);
//        VisCardMessage vis = null;
//        try {
//            vis = new ObjectMapper().readValue(object, VisCardMessage.class);
//            //更新访客状态
//            VisitorAppointment visitor = visitorAppointmentService.getByIDCarNumber(vis.getUser_number());
//            visitor.setVisitorStatus("已入场");
//            //新增访客记录
//            VisitorList record = new VisitorList();
//            record.setVisitorName(vis.getUser_name());
//            record.setVisitorCard(vis.getUser_number());
//            record.setVisitorTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//            visitorRecordListService.add(record);
//        } catch (Exception e) {
//            log.info("设备端上传访客人证记录失败！"+object);
//        }
//        return Restful.success("新增访客预约");
//    }

    @ApiOperation("访客签离")
    @PostMapping("/v1/visitLeave")
    public Restful<VisReservation> recordVisit(@Parameter(description = "设备编号")@RequestParam("sn") String sn,
                                               @Parameter(description = "访客用户id")@RequestParam("user_id") String user_id,
                                               @Parameter(description = "访客身份证号码")@RequestParam("id_number") String id_number) {
        log.info("设备端上传离开访客身份证号码"+id_number);
        //更新访客状态
        VisitorAppointment visitor = visitorAppointmentService.getByIDCardNumber(id_number);
        visitor.setVisitorStatus("已出场");
        //更新访客记录
        VisitorList record = visitorRecordListService.getByIdCard(id_number);
        record.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        visitorRecordListService.update(record,record.getId());

        return Restful.success("访客离开");
    }


//    @ApiOperation("获取访客权限组集合")
//    @GetMapping("/visLevel/list")
//    public Response<PageInfo<VisitorAppointment>> getVistorAppointment(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
//                                                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                       @RequestParam String apitoken) {
//        PageInfo<VisitorAppointment> pageInfo = vistorAppointmentService.get(pageNo, pageSize);
//        return Response.success("获取成功！", pageInfo);
//    }

//    Alcohol alcohol = null;
//        alcohol = new ObjectMapper().readValue(object, Alcohol.class);

}
